package net.assassin8.bautista.stalker;

import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MallDownloadActivity extends Activity {

	private Stalker stalker;

	private final Context ctx = this;

	private String address;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		stalker = ((Stalker) getApplicationContext());
		initMallSelector();
	}

	public void initMallSelector() {
		// Create the layout of this activity
		LinearLayout layout = new LinearLayout(this);
		layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT));
		layout.setOrientation(LinearLayout.VERTICAL);

		// Create the title text view
		TextView title = new TextView(this);
		title.setText("Malls on the server");
		title.setTextSize(35);
		title.setPadding(0, 0, 0, 10);
		title.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT));
		title.setMinHeight(40);
		layout.addView(title);

		// Define mall buttons array
		Button mallButtons[];

		// Grab mall content off the server
		try {
			URL url = new URL("http://webstalk.bautista.me/stalker/get_malls_xml");
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(new InputSource(url.openStream()));
			doc.getDocumentElement().normalize();

			NodeList nodeList = doc.getElementsByTagName("mall");

			/** Assign textview array lenght by arraylist size */
			mallButtons = new Button[nodeList.getLength()];

			for (int i = 0; i < nodeList.getLength(); i++) {
				// Make buttons first

				Node node = nodeList.item(i);

				// address description number_of_floors

				mallButtons[i] = new Button(this);

				Element fstElmnt = (Element) node;
				NodeList idList = fstElmnt.getElementsByTagName("id");
				Element idElement = (Element) idList.item(0);
				idList = idElement.getChildNodes();
				final long id = Long.parseLong(((Node) idList.item(0))
						.getNodeValue());

				NodeList nameList = fstElmnt.getElementsByTagName("name");
				Element nameElement = (Element) nameList.item(0);
				nameList = nameElement.getChildNodes();
				final String name = ((Node) nameList.item(0)).getNodeValue();
				
				NodeList addressList = fstElmnt.getElementsByTagName("address");
				Element addressElement = (Element) addressList.item(0);
				addressList = addressElement.getChildNodes();
				address = "";
				try {
					address = ((Node) addressList.item(0))
							.getNodeValue();
				} catch (Exception e) {

				}

				
				NodeList floorsList = fstElmnt.getElementsByTagName("floors");
				Element floorElement = (Element) floorsList.item(0);
				floorsList = floorElement.getChildNodes();
				final String floors = ((Node) floorsList.item(0)).getNodeValue();
				
				NodeList descriptionList = fstElmnt.getElementsByTagName("description");
				Element descriptionElement = (Element) descriptionList.item(0);
				descriptionList = descriptionElement.getChildNodes();
				final String description = ((Node) descriptionList.item(0)).getNodeValue();
				
				mallButtons[i].setText(name);
				mallButtons[i].setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// Show the mall's details
						Intent i = new Intent(MallDownloadActivity.this,
								MallDownloadDetailsActivity.class);
						i.putExtra("MALL_ID", id);
						i.putExtra("MALL_NAME", name);
						i.putExtra("MALL_ADDRESS", address);
						i.putExtra("MALL_FLOORS", floors);
						i.putExtra("MALL_DESCRIPTION", description);
						MallDownloadActivity.this.startActivity(i);
						MallDownloadActivity.this.finish();
					}

				});
				mallButtons[i].setLayoutParams(new LayoutParams(
						LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

				layout.addView(mallButtons[i]);
			}
		} catch (Exception e) {
			System.out.println("XML Pasing Excpetion = " + e);
			stalker.alert(ctx, "Failed to connect to server");
		}

		/** Set the layout view to display */
		setContentView(layout);
	}

}
